package com.good.akkaserver.server;

import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.good.akkaserver.event.ClientEvent;
import com.good.akkaserver.model.PlayerSession;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.Attribute;

import static com.good.akkaserver.constant.AttributeMapConstant.NETTY_CHANNEL_KEY;  

public class CommServerHandler extends SimpleChannelInboundHandler	{
	
	private static Logger logger = LoggerFactory.getLogger(CommServerHandler.class);
	
	private static AkkaServer akkaServer = AkkaServer.getInstance();
	private static ObjectPoolWorld objectPoolWorld = ObjectPoolWorld.getInstance();
	
	
	/** 用于生成  playerSessonID */
	private static final AtomicInteger PLAYER_SESSION = new AtomicInteger(1000);
	
	@Override
    public void messageReceived(final ChannelHandlerContext ctx, Object msg) {
		String buf = (String)msg;
		logger.info("receive client DataContext,length:[" + buf.length()+"],Data:"+buf);
		ClientEvent event = objectPoolWorld.getClientEvent(); 			// 可以使用对象池
		event.setTimestamp(System.currentTimeMillis());					// 收到时的时间
		Attribute<PlayerSession> attr = ctx.attr(NETTY_CHANNEL_KEY);  
		PlayerSession session = attr.get();  
        if (session != null) {  
        	event.setSessionID(session.getId());
        }
		event.setConData(buf.getBytes());
		akkaServer.dispatchClientEvent(event);
		
		
	}
	
}
